using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Entities;
namespace DAL
{
   public class tbTourthamgiaDAL
    {
        private SqlConnection cn = null;
        public tbTourthamgiaDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbTourthamgia ttg)
        {
            SqlCommand cm = new SqlCommand("sp_tourthamgia_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matourmo",ttg.Matourmo);
            cm.Parameters.AddWithValue("@makh",ttg.Makh );
            cm.Parameters.AddWithValue("@soluongnguoilon", ttg.Soluongnguoilon);
            cm.Parameters.AddWithValue("@soluongnguoinho", ttg.Soluongnguoinho);
            cm.ExecuteNonQuery();
        }
        public void Update(tbTourthamgia ttg)
        {
            SqlCommand cm = new SqlCommand("sp_tourthamgia_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matourthamgia", ttg.Matourthamgia);
            cm.Parameters.AddWithValue("@matourmo", ttg.Matourmo);
            cm.Parameters.AddWithValue("@makh", ttg.Makh);
            cm.Parameters.AddWithValue("@soluongnguoilon", ttg.Soluongnguoilon);
            cm.Parameters.AddWithValue("@soluongnguoinho", ttg.Soluongnguoinho);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tourthamgia_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matourthamgia", iD);
            cm.ExecuteNonQuery();
        }
        public tbTourtthamgiaCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tourthamgia_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbTourtthamgiaCollection ttgColl = new tbTourtthamgiaCollection();

            while (dr.Read()) 
            {
                tbTourthamgia tt = new tbTourthamgia();
                tt.Matourthamgia = int.Parse(dr.GetValue(0).ToString());
                tt.Matourmo = int.Parse(dr.GetValue(1).ToString());
                tt.Makh = int.Parse(dr.GetValue(2).ToString());
                tt.Soluongnguoilon = int.Parse(dr.GetValue(3).ToString());
                tt.Soluongnguoinho = int.Parse(dr.GetValue(4).ToString());
                ttgColl.Add(tt);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ttgColl;
        }
    }
}
